package com.`fun`.funlivewallpaper

import android.Manifest
import android.annotation.SuppressLint
import android.app.WallpaperManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.funny.wallpaper.FunnyWallpaperHelper

import com.guzhen.wallpager.Live1WallpaperService
import com.guzhen.wallpager.ReviewLiveWallpaperService
import java.io.IOException
import java.lang.ref.WeakReference

class MainActivity : AppCompatActivity() {
    companion object{
        var main:WeakReference<MainActivity>? = null
    }


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        main = WeakReference(this)
        setContentView(R.layout.activity_main)





        findViewById<View>(R.id.lottie_v).setOnClickListener{
            val a = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)

            if (a == PackageManager.PERMISSION_GRANTED){

                setup()
            }else{
                requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), 29031)
            }
        }

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 29031) {
            val a = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            if (a == PackageManager.PERMISSION_GRANTED){
                setup()
            }
        }
    }

    private fun setup() {
        WallpaperHelper.saveVideoAndStartWallpaper(this) {
//            setToWallPaper(this)
            FunnyWallpaperHelper.setToWallPaper(this)
        }
//        setupService()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        main?.clear()
        main = null
    }

    override fun finish() {
        super.finish()
        main?.clear()
        main = null
    }

    override fun onDestroy() {
        super.onDestroy()
        main?.clear()
        main = null
    }

    fun setToWallPaper(context: Context) {
        try {
//            WallpaperManager.getInstance(context)
//                .clear()
        } catch (e: IOException) {
        }

        val intent = Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
        intent.putExtra(
            WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
            ComponentName(context, ReviewLiveWallpaperService::class.java)
        )
        intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(intent)
    }
    private var isBind = false
    private fun setupService() {
        this?.let { _context ->
            val intent = Intent(_context, StepService::class.java)
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                _context.startForegroundService(intent)
            } else {
                _context.startService(intent)
            }
        }
    }
}